package br.org.etec.dotcao.model;

import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;

import org.codehaus.jackson.map.annotate.JsonDeserialize;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import org.hibernate.validator.constraints.NotBlank;

import br.org.etec.dotcao.ws.utils.JsonDateDeserializer;
import br.org.etec.dotcao.ws.utils.JsonDateSerializer;


@NamedQueries({@NamedQuery(name="selectNews", query="select ns from News ns where ns.title = :titleparam or ns.date = :date")})
@Entity
@Table(name="tnews")
public class News extends AbstractEntity {

	private static final long serialVersionUID = 1L;

	@Column
	@NotNull
	private String title;

	@Column
	@NotNull
	@NotBlank
	private String content;

	@Temporal(TemporalType.TIMESTAMP)
	@NotNull
	private Date date;

	@ManyToOne
	@JoinColumn(name = "sender")
	private User sender;

	@OneToMany(cascade = CascadeType.ALL, mappedBy = "news")
	private List<NewsComment> comments;

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	@JsonSerialize(using=JsonDateSerializer.class, as=Date.class)
	public Date getDate() {
		return date;
	}

	@JsonDeserialize(using=JsonDateDeserializer.class, as=Date.class)
	public void setDate(Date date) {
		this.date = date;
	}

	public User getSender() {
		return sender;
	}

	public void setSender(User sender) {
		this.sender = sender;
	}

	public List<NewsComment> getComments() {
		return comments;
	}

	public void setComments(List<NewsComment> comments) {
		this.comments = comments;
	}

}
